package com.itheima.app.log;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Log;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TanHuaLog {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Around("execution(* com.itheima.app.manager.*.*(..))")
    public Object aroundMethod(ProceedingJoinPoint pjp) {
        Object obj = null;
        try {
            obj = pjp.proceed();

            Log log = new Log();
            log.setLogTime(DateUtil.today());
            log.setPlace("北京大兴");
            log.setEquipment("华为P40");
            MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
            String name = methodSignature.getName();
            if (StrUtil.equals(name, "saveMovement")) {
                log.setType("0201");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "findRecommendMovement") || StrUtil.equals(name, "findMovementVo") || StrUtil.equals(name, "findFriendMovement")) {
                log.setType("0202");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "commentLike")) {
                log.setType("0203");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "saveMovementLove")) {
                log.setType("0204");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "saveComment")) {
                log.setType("0205");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "commentDisLike")) {
                log.setType("0206");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "deleteMovementLove")) {
                log.setType("0207");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "saveSmallVideos")) {
                log.setType("0301");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "saveLike")) {
                log.setType("0302");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "removeDisLike")) {
                log.setType("0303");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            } else if (StrUtil.equals(name, "saveVideoComment")) {
                log.setType("0304");
                log.setUserId(UserHolder.getId());
                rocketMQTemplate.convertAndSend("tanhua-log", log);
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return obj;
    }

}
